package com.blog.controller;

import com.blog.common.ServerResponse;
import com.blog.pojo.Article;
import com.blog.pojo.Category;
import com.blog.service.IArticleService;
import com.blog.service.ICategoryService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;


/**
 * @author
 * @create 2018-06-04 15:09
 **/
@Controller
@RequestMapping("/front/")
public class FrontController {

    private Logger log = Logger.getLogger(this.getClass());
    @Autowired
    private IArticleService articleService;

    @Autowired
    private ICategoryService categoryService;

    /**
     * 查询所有文章
     *
     * @param map 视图
     * @return
     */
    @RequestMapping("index.action")
    public String list(ModelMap map,
                       @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
                       @RequestParam(value = "pageSize", defaultValue = "5") int pageSize) {
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("status", "1");
        // pageHelper分页插件
        // 只需要在查询之前调用，传入当前页码，以及每一页显示多少条
        PageHelper.startPage(pageNum, pageSize);
        //不改变查询
        List<Article> articlesList = articleService.list(param);
        PageInfo<Article> pageInfo = new PageInfo<Article>(articlesList);
        map.put("pageInfo", pageInfo);
        //查询所有文章，放进去
        return "portal/index";
    }

    /**
     * 查询文章详情页面内容
     * @param id   文章id
     * @param map   模式与视图
     * @return
     */
    @RequestMapping("content.action")
    public String content(@RequestParam(value = "id") String id, ModelMap map) {
            Article article = articleService.selectById(id);
        int viewCount = article.getViewCount();
        /*浏览次数+1*/
        viewCount++;
        article.setViewCount(viewCount);
        articleService.update(article);
            map.put("article", article);
        return "portal/single";
    }



}
